package com.ruoyi.partybuilding.controller;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.*;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import lombok.var;
import com.ruoyi.common.core.domain.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.partybuilding.domain.Message;
//import com.ruoyi.partybuilding.service.MessageService;
import com.ruoyi.partybuilding.common.*;

/**message
 * 消息;(pb_message)表控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2024-12-5
 */
@Api(tags = "消息对象功能接口")
@RestController
@RequestMapping("/partybuilding/Message")
public class MessageController extends EQueryController<Message>{

    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('partybuilding:Message:query')")
    @GetMapping(value = "/{id}")
    public R<Message> getInfo(@PathVariable("id") Long id)
    {
        return super.getInfo(id);
    }
    @ApiOperation("分页")
    @PreAuthorize("@ss.hasPermi('partybuilding:Message:list')")
    @GetMapping("query")
    public R<Page<Message>> query(Message whereObj, PageDomain pageRequest) {
        return super.queryPage(whereObj, pageRequest);
    }
    @ApiOperation("列表,对query的rouyi兼容包装")
    @PreAuthorize("@ss.hasPermi('partybuilding:Message:list')")
    @GetMapping("list")
    public TableDataInfo list(Message entity, PageSort pageRequest) {
        var page= super.queryPage(entity, pageRequest);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getData().getContent());
        rspData.setTotal(page.getData().getTotalElements());
        return rspData;
    }

    @ApiOperation(value = "我的消息",notes = "自动使用当前登录用户id过滤")
    @GetMapping("myMessages")
    public TableDataInfoRows<Message> myMessages(Message entity, PageSort pageRequest) {
        entity.setUserId(getUserId());
        var page = super.queryPage(entity, pageRequest);
        return toTableDataInfoRows(page);
    }
    @ApiOperation("设置已读")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PostMapping("readSet")
    public  R<Long> readSet(Long id) {
        var re = super.getInfo(id);
        var entity = re.getData();
        if(entity.getUserId()!=getUserId())
            return R.fail("已读人错误");
        entity.setStatus(1l);
        var rm=entity.getRemark()+";readSet:"+ DateUtil.now();
        entity.setRemark(rm);
        return super.edit(entity);
    }
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('partybuilding:Message:add')")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public R<Long> add(@RequestBody Message entity){
        return super.add(entity);
    }
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('partybuilding:Message:remove')")
    @Log(title = "消息", businessType = BusinessType.DELETE)
    @PostMapping("remove/{ids}")
    public R<Long> remove(@PathVariable("ids") Long[] ids){
        return super.remove(Arrays.stream(ids).toArray());
    }
    @ApiOperation("编辑")
    @PreAuthorize("@ss.hasPermi('partybuilding:Message:edit')")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public  R<Long> edit(@RequestBody Message entity){
        return super.edit(entity);
    }
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('partybuilding:Message:export')")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Message entity, PageDomain pageRequest,HttpServletResponse response)
    {
        super.export(entity,pageRequest,null,response);
    }
}